package org.example.config.conditional.test.config;

import org.example.config.conditional.apm.ApmAutoConfig;
import org.example.config.conditional.apm.beans.MockUser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.*;

@Configuration
// 扫描 @SpringBootApplication 所在包之外的路径
@ComponentScan(basePackages = "org.example.config.conditional.apm.beans")
// @Import优先级高于@Bean
@Import(ApmAutoConfig.class)
public class MockAutoConfig {
    // 下面 @Profile 和 @ConditionalOnProperty 两个实现功能相同，都是仅在测试环境下创建mockUser1 Bean。
    @Profile("test")
    @ConditionalOnProperty(prefix = "spring.profiles", name = "active", havingValue = "test")
    @Bean
    // 当没有其他类型为MockUser的BeanDefinition时，才创建此mockUser1 Bean。
    @ConditionalOnMissingBean(MockUser.class)
    public MockUser mockUser1() {
        return new MockUser();
    }
}